package com.sz.jinzheng.controller;


import com.sz.jinzheng.model.BorrowInfo;
import com.sz.jinzheng.model.BorrowerInfo;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.service.BorrowInfoService;
import com.sz.jinzheng.service.UserService;
import com.sz.jinzheng.until.JwtUntil;
import com.sz.jinzheng.until.UserUntil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class BorrowInfoController {
    @Autowired
    private BorrowInfoService borrowInfoService;
    @Resource
    private UserService userService;
    @Resource
    private UserUntil userUntil;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JwtUntil jwtUntil;

    /**
     * 通过token初始化用户所有的贷款信息
     *
     * @return
     */
    @GetMapping("/allBorrowerInfo")
    public Result allBorrowerInfo(String token) {
        Claims claims = jwtUntil.parseToken(token);
        if (claims != null) {
            //1.获取到手机号
            String phone = claims.getSubject();
            //2.通过手机号查询uid
            Integer uid = userService.getUserByPhone(phone).getUid();
            //3.通过uid查询借款人信息表
            List<BorrowerInfo> borrowerInfo = borrowInfoService.allBorrowerInfo(uid);
            System.out.println(borrowerInfo);
            if (borrowerInfo.size() == 0) {
                return new Result(333, "用户无贷款", "用户无贷款");
            }
            return new Result(200, "获取成功", borrowerInfo);
        }
        return new Result(444, "查询失败", "token过期");
    }


    /**
     * 通过token初始化用户所有审核通过的贷款信息
     *
     * @return
     */
    @GetMapping("/allBorrowerInfo1")
    public Result allBorrowerInfo1(String token) {
        Claims claims = jwtUntil.parseToken(token);
        if (claims != null) {
            //1.获取到手机号
            String phone = claims.getSubject();
            //2.通过手机号查询uid
            Integer uid = userService.getUserByPhone(phone).getUid();
            //3.通过uid查询借款人信息表
            List<BorrowerInfo> borrowerInfo = borrowInfoService.allBorrowInfo1(uid);
            System.out.println(borrowerInfo);
            if (borrowerInfo.size() == 0) {
                return new Result(333, "用户无贷款通过的记录", "用户无贷款通过的记录");
            }
            return new Result(200, "获取成功", borrowerInfo);
        }
        return new Result(444, "查询失败", "token过期");
    }

    /**
     * 获取通过审核的贷款
     *
     * @param token
     * @return
     */
    @GetMapping("/tongGuoBorrowerInfo")
    public Result tongGuoBorrowerInfo(String token) {
        System.out.println("token = " + token);
        Integer uid = jwtUntil.getUid(token);
        if (uid == null) {
            return new Result(444, "tonken过期", "tonken过期");
        }
        List<BorrowerInfo> borrowerInfo = borrowInfoService.tongGuoBorrowerInfo(uid);
        if (borrowerInfo.size() == 0) {
            return new Result(333, "用户无贷款", "用户无贷款");
        }
        return new Result(200, "获取成功", borrowerInfo);
    }

    /**
     * 获取未通过审核的贷款
     *
     * @param token
     * @return
     */
    @GetMapping("/notBorrowerInfo")
    public Result notBorrowerInfo(String token) {
        Integer uid = jwtUntil.getUid(token);
        if (uid == null) {
            return new Result(444, "tonken过期", "tonken过期");
        }
        List<BorrowerInfo> borrowerInfo = borrowInfoService.notBorrowerInfo(uid);
        if (borrowerInfo.size() == 0) {
            return new Result(333, "用户无贷款", "用户无贷款");
        }
        return new Result(200, "获取成功", borrowerInfo);
    }

    /**
     * 获取结清的贷款
     *
     * @param token
     * @return
     */
    @GetMapping("/jieQingBorrowerInfo")
    public Result jieQingBorrowerInfo(String token) {
        Integer uid = jwtUntil.getUid(token);
        if (uid == null) {
            return new Result(444, "tonken过期", "tonken过期");
        }
        List<BorrowerInfo> borrowerInfo = borrowInfoService.jieQingBorrowerInfo(uid);
        if (borrowerInfo.size() == 0) {
            return new Result(333, "用户无贷款", "用户无贷款");
        }
        return new Result(200, "获取成功", borrowerInfo);
    }

    /**
     * 获取未结清的贷款
     *
     * @param token
     * @return
     */
    @GetMapping("/notQingBorrowerInfo")
    public Result notQingBorrowerInfo(String token) {
        Integer uid = jwtUntil.getUid(token);
        if (uid == null) {
            return new Result(444, "tonken过期", "tonken过期");
        }
        List<BorrowerInfo> borrowerInfo = borrowInfoService.notQingBorrowerInfo(uid);
        if (borrowerInfo.size() == 0) {
            return new Result(333, "用户无贷款", "用户无贷款");
        }
        return new Result(200, "获取成功", borrowerInfo);
    }


    /**
     * 查看账单表
     *
     * @param token      用于验证user
     * @param billFormId 借款信息表id
     * @return
     */
    @PostMapping("/showBorrowInfo")
    public Result showBorrowInfo(String token, Integer billFormId) {
        System.out.println(token);
        System.out.println(billFormId);
        //1.获取uid
        Integer uid = jwtUntil.getUid(token);
        //2.查询账单
        return null;
    }


    @GetMapping("/getApprovalComments")
    public String getApprovalComments(Integer borrowerInfoId) {
        //查看未通过的原因
        System.out.println("borrowerInfoId = " + borrowerInfoId);
        return borrowInfoService.getApprovalComments(borrowerInfoId);

    }
}
